package io.renren.modules.noc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.noc.dao.NocResultDao;
import io.renren.modules.noc.dao.NocRoleDao;
import io.renren.modules.noc.entity.NocRoleEntity;
import io.renren.modules.noc.service.NocResultService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("nocResultService")
public class NocResultServiceImpl implements NocResultService {
    SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-DD");

    @Autowired
    private NocResultDao nocResultDao;
    @Autowired
    private NocRoleDao roleDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private NocRoleDao nocRoleDao;

    @Override
    public List<Map<String,Object>> getTotalResult(String tid, String start, String end){
        return nocResultDao.getTotalResult(tid,start,end);
    }

    @Override
    public List<Map<String,Object>> getNewResult(String tid, String start, String end){
        List<Map<String,Object>> list = nocResultDao.getNewResult(tid,start,end);
        for(Map<String,Object> map:list){
            String days = (String)map.get("days");
            List<Map<String,Object>> recomList = nocResultDao.getRecomeResult(tid,start,days);
            for(Map<String,Object> m:recomList){
                map.put("recomePerson",m.get("totalPerson"));
                map.put("recomeCompany",m.get("totalCompany"));
            }
        }
        return list;
    }

    @Override
    public List<Map<String,Object>> getRecomeResult(String tid, String start, String end){
        return nocResultDao.getRecomeResult(tid,start,end);
    }

    @Override
    public List<Map<String,Object>> getMemberReceipt(Page<Map<String,Object>> page, Map<String, Object> params){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        String deptId = (String) params.get("deptId");
        //获取系统用户的来源权限
        Map<String,Object> roleMap = new HashMap<>();
        roleMap.put("tid",params.get("tid"));
        roleMap.put("uid",userEntity.getUserId());
        List<String> in = roleDao.getUserLyRole(roleMap);

        //根据部门取用户取来源
        if(StringUtils.isNotEmpty(deptId)){
            Map<String,Object> userMap = new HashMap<>();
            userMap.put("dept_id",deptId);
            List<SysUserEntity> userList = sysUserDao.selectByMap(userMap);
            List<Long> userStrList = new ArrayList<>();
            for(SysUserEntity user: userList) {
                userStrList.add(user.getUserId());
            }
            List<NocRoleEntity> roleList = nocRoleDao.selectList(
                    new QueryWrapper<NocRoleEntity>()
                            .in("user_id",userStrList)
            );
            for(NocRoleEntity nocRole : roleList){
                in.add(nocRole.getLy());
            }
        }

        if(in!=null&&in.size()>0) {
            params.put("lys", in);
        }
        return nocResultDao.getMemberReceipt(page,params);
    }

    @Override
    public Map<String,Object> getCompanyReceipt(Map<String, Object> params){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        //获取系统用户的来源权限
        Map<String,Object> roleMap = new HashMap<>();
        roleMap.put("tid",params.get("tid"));
        roleMap.put("uid",userEntity.getUserId());
        List<String> in = roleDao.getUserLyRole(roleMap);
        if(in!=null&&in.size()>0) {
            params.put("lys", in);
        }
        return nocResultDao.getCompanyReceipt(params);
    }

    @Override
    public List<Map<String, Object>> getMemberPresent(Page<Map<String, Object>> page, Map<String, Object> params) {
        return nocResultDao.getMemberPresent(page,params);
    }

    @Override
    public List<Map<String, Object>> getAudiencePresent(Page<Map<String, Object>> page, Map<String, Object> params) {
        return nocResultDao.getAudiencePresent(page,params);
    }
}
